<!DOCTYPE html>
<html dir="ltr">

<head>
  <meta charset="UTF-8">
  <title>Refresher - Basic</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <link href="../../../../../css/ionic.bundle.css" rel="stylesheet">
  <link href="../../../../../scripts/testing/styles.css" rel="stylesheet">
  <script src="../../../../../scripts/testing/scripts.js"></script>
  <script nomodule src="../../../../../dist/ionic/ionic.js"></script>
  <script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script></head>

<body>
  <ion-app>
    <ion-header>
      <ion-toolbar>
        <ion-title>Pull To Refresh</ion-title>
      </ion-toolbar>
    </ion-header>

    <ion-content>
      <ion-refresher id="refresher" slot="fixed">
        <ion-refresher-content pulling-text="Pull to refresh..." refreshing-text="Refreshing...">
        </ion-refresher-content>
      </ion-refresher>

      <ion-list id="list"></ion-list>
    </ion-content>
  </ion-app>
  <ion-menu-controller></ion-menu-controller>

  <script>

    let items = [];
    for (var i = 0; i < 30; i++) {
      items.push(i + 1);
    }
    const list = document.getElementById('list');
    const refresher = document.getElementById('refresher');
    refresher.addEventListener('ionRefresh', async function () {
      console.log('Loading data...');
      const data = await getAsyncData();
      items = items.concat(data);
      refresher.complete();
      render();
      console.log('Done');
    });
    function render() {
      let html = '';
      for (let item of items) {
        html += `<ion-item button>${item}</ion-item>`;
      }
      list.innerHTML = html;
    }
    function getAsyncData() {
      // async return mock data
      return new Promise(resolve => {
        setTimeout(() => {
          let data = [];
          for (var i = 0; i < 30; i++) {
            data.push(i);
          }
          resolve(data);
        }, 500);
      });
    }
    render();
  </script>
</body>

</html>
